package org.example.ui.employee;

import org.example.manage.OrderManage;
import org.example.model.Order;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.List;

public class ManageOrderPanel extends JPanel {
    private JLabel[] topLabel = new JLabel[7];
    private SpringLayout layout = new SpringLayout();
    private static SpringLayout teaLayout = new SpringLayout();
    private JCheckBox box;
    private static JPanel teaPanel = new JPanel();
    private static ManageOrderItem[] manageOrderItems;
    private String[] topText = {"      订单号      ", "               取餐人                  ",
            "                     商品列表                     ", "           数量        ", "       总价格       ", "         状态       ",
            "     操作     "};
    private static List<Order> list;

    public ManageOrderPanel() {
        setLayout(layout);
        JTable table = new JTable();
        TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
        box = (JCheckBox) renderer;
        Image image2 = new ImageIcon("data/background/bag_icon.jpg").getImage().
                getScaledInstance(10, 10, Image.SCALE_DEFAULT);
        box.setIcon(new ImageIcon(image2));
        box.setSelectedIcon(new ImageIcon(image2));
        add(box);
        teaPanel.setLayout(teaLayout);
        setBorder(BorderFactory.createLoweredSoftBevelBorder());
        JScrollPane scrollPane = new JScrollPane(teaPanel);
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        teaPanel.setBackground(null);
        teaPanel.setOpaque(false);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.getVerticalScrollBar().setUnitIncrement(20);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        add(scrollPane);
        layout.putConstraint(SpringLayout.WEST, box, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, box, 60, SpringLayout.NORTH, this);
        Font font = new Font("", Font.BOLD, 15);
        for (int i = 0; i < 7; i++) {
            topLabel[i] = new JLabel(topText[i], JLabel.CENTER);
            topLabel[i].setFont(font);
            topLabel[i].setOpaque(true);
            topLabel[i].setBackground(Color.WHITE);
            topLabel[i].setBorder(null);
            add(topLabel[i]);
            if (i == 0) {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 3, SpringLayout.EAST, box);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 60, SpringLayout.NORTH, this);
            } else {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 0, SpringLayout.EAST, topLabel[i - 1]);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 60, SpringLayout.NORTH, this);
            }
            if (i == 1) {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 0, SpringLayout.EAST, topLabel[i - 1]);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 60, SpringLayout.NORTH, this);
            }
            if (i == 2) {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 0, SpringLayout.EAST, topLabel[i - 1]);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 60, SpringLayout.NORTH, this);
            }
            if (i == 3) {
                layout.putConstraint(SpringLayout.WEST, topLabel[i], 0, SpringLayout.EAST, topLabel[i - 1]);
                layout.putConstraint(SpringLayout.NORTH, topLabel[i], 60, SpringLayout.NORTH, this);
            }
            if (i == 6) {
                layout.putConstraint(SpringLayout.EAST, topLabel[i], 60, SpringLayout.EAST, this);
            }
        }
        setAdminItems();
        layout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, scrollPane, 10, SpringLayout.SOUTH, topLabel[1]);
        layout.putConstraint(SpringLayout.SOUTH, scrollPane, -10, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, this);
        setVisible(true);
    }

    public static void setAdminItems() {
        list = new OrderManage("data/order_file/need_do_order.json").read();
        teaPanel.removeAll();
        manageOrderItems = new ManageOrderItem[list.size()];
        for (int i = 0; i < manageOrderItems.length; i++) {
            manageOrderItems[i] = new ManageOrderItem(list.get(i), i + 1);
            teaPanel.add(manageOrderItems[i]);
            if (i == 0) {
                teaLayout.putConstraint(SpringLayout.NORTH, manageOrderItems[i], 0, SpringLayout.NORTH, teaPanel);
                teaLayout.putConstraint(SpringLayout.SOUTH, manageOrderItems[i], 100, SpringLayout.NORTH, teaPanel);
            } else {
                teaLayout.putConstraint(SpringLayout.NORTH, manageOrderItems[i], 0, SpringLayout.SOUTH, manageOrderItems[i - 1]);
                teaLayout.putConstraint(SpringLayout.SOUTH, manageOrderItems[i], 100, SpringLayout.SOUTH, manageOrderItems[i - 1]);
            }
            teaLayout.putConstraint(SpringLayout.WEST, manageOrderItems[i], 0, SpringLayout.WEST, teaPanel);
            teaLayout.putConstraint(SpringLayout.EAST, manageOrderItems[i], 0, SpringLayout.EAST, teaPanel);
        }
        teaPanel.setPreferredSize(new Dimension(300, 105 * list.size()));
        teaPanel.setVisible(false);
        teaPanel.setVisible(true);
    }
}
